﻿using Microsoft.EntityFrameworkCore;
using mozhi.smarterp.Business.Inventories;
using mozhi.smarterp.Business.Products;
using mozhi.smarterp.Business.Purchases;
using mozhi.smarterp.Business.Sales;
using Volo.Abp;
using Volo.Abp.EntityFrameworkCore.Modeling;

namespace mozhi.smarterp.Business.EntityFrameworkCore;

public static class BusinessDbContextModelCreatingExtensions
{
    public static void ConfigureBusiness(
        this ModelBuilder builder)
    {
        Check.NotNull(builder, nameof(builder));

        /* Configure all entities here. Example:

        builder.Entity<Question>(b =>
        {
            //Configure table & schema name
            b.ToTable(BusinessDbProperties.DbTablePrefix + "Questions", BusinessDbProperties.DbSchema);

            b.ConfigureByConvention();

            //Properties
            b.Property(q => q.Title).IsRequired().HasMaxLength(QuestionConsts.MaxTitleLength);

            //Relations
            b.HasMany(question => question.Tags).WithOne().HasForeignKey(qt => qt.QuestionId);

            //Indexes
            b.HasIndex(q => q.CreationTime);
        });
        */

        builder.Entity<InventoryRealTime>(b =>
        {
            b.ToTable(BusinessDbProperties.DbTablePrefix + "InventoryRealTime", BusinessDbProperties.DbSchema);
            b.Ignore(x=>x.Product);
            b.Ignore(x => x.Warehouse);

            b.ConfigureByConvention();
        });

        builder.Entity<ProductInventorySetting>(b =>
        {
            b.ToTable(BusinessDbProperties.DbTablePrefix + "ProductInventorySetting", BusinessDbProperties.DbSchema);
            b.Ignore(x => x.Product);
            b.Ignore(x => x.DefaultWarehouse);

            b.ConfigureByConvention();
        });

        builder.Entity<ProductPurchaseSetting>(b =>
        {
            b.ToTable(BusinessDbProperties.DbTablePrefix + "ProductPurchaseSetting", BusinessDbProperties.DbSchema);
            b.Ignore(x => x.Product);
            b.Ignore(x => x.DefaultPurchaser);
            b.Ignore(x => x.DefaultSupplier);
            b.Ignore(x => x.PurchaseUnit);
            b.Ignore(x => x.ProductAuxiliaryUnit);

            b.ConfigureByConvention();
        });

        builder.Entity<PurchaseRequisition>(b =>
        {
            b.ToTable(BusinessDbProperties.DbTablePrefix + "PurchaseRequisition", BusinessDbProperties.DbSchema);
            b.HasIndex(x => new { x.Number, x.TenantId }).IsUnique();
            b.Property(b => b.ExchangeRate).HasColumnType("decimal(18, 10)");
            b.Property(b => b.TotalAmount).HasColumnType("decimal(18, 2)");
            b.Property(b => b.TotalAmountFr).HasColumnType("decimal(18, 2)");
            b.Property(b => b.Amount).HasColumnType("decimal(18, 2)");
            b.Property(b => b.AmountFr).HasColumnType("decimal(18, 2)");
            b.Property(b => b.TaxAmount).HasColumnType("decimal(18, 2)");
            b.Property(b => b.TaxAmountFr).HasColumnType("decimal(18, 2)");
            b.Property(b => b.TotalQuantity).HasColumnType("decimal(18, 10)");
            b.Property(b => b.OrderQuantity).HasColumnType("decimal(18, 10)");
            b.Ignore(x => x.Department);
            b.Ignore(x => x.Applicant);
            b.Ignore(x=>x.Currency);

            b.ConfigureByConvention();
        });


        builder.Entity<PurchaseRequisitionItem>(b =>
        {
            b.ToTable(BusinessDbProperties.DbTablePrefix + "PurchaseRequisitionItem", BusinessDbProperties.DbSchema);
            b.Property(b => b.AuxiliaryQuantity).HasColumnType("decimal(18, 10)");
            b.Property(b => b.BasicQuantity).HasColumnType("decimal(18, 10)");
            b.Property(b => b.Price).HasColumnType("decimal(18, 10)");
            b.Property(b => b.PriceExcludeTax).HasColumnType("decimal(18, 10)");
            b.Property(b => b.PriceIncludeTax).HasColumnType("decimal(18, 10)");
            b.Property(b => b.TaxRateValue).HasColumnType("decimal(18, 10)");
            b.Property(b => b.PriceIncludeTax).HasColumnType("decimal(18, 10)");
            b.Property(b => b.TotalAmount).HasColumnType("decimal(18, 2)");
            b.Property(b => b.TotalAmountFr).HasColumnType("decimal(18, 2)");
            b.Property(b => b.Amount).HasColumnType("decimal(18, 2)");
            b.Property(b => b.AmountFr).HasColumnType("decimal(18, 2)");
            b.Property(b => b.TaxAmount).HasColumnType("decimal(18, 2)");
            b.Property(b => b.TaxAmountFr).HasColumnType("decimal(18, 2)");
            b.Ignore(x => x.Product);
            b.Ignore(x => x.ProductAuxiliaryUnit);
            b.Ignore(x => x.AuxiliaryUnit);
            b.Ignore(x => x.TaxRate);
            b.Ignore(x => x.SuggestedSupplier);

            b.ConfigureByConvention();
        });

        builder.Entity<PurchaseOrder>(b =>
        {
            b.ToTable(BusinessDbProperties.DbTablePrefix + "PurchaseOrder", BusinessDbProperties.DbSchema);
            b.HasIndex(x => new { x.Number, x.TenantId }).IsUnique();
            b.Property(b => b.ExchangeRate).HasColumnType("decimal(18, 10)");
            b.Property(b => b.TotalAmount).HasColumnType("decimal(18, 2)");
            b.Property(b => b.TotalAmountFr).HasColumnType("decimal(18, 2)");
            b.Property(b => b.Amount).HasColumnType("decimal(18, 2)");
            b.Property(b => b.AmountFr).HasColumnType("decimal(18, 2)");
            b.Property(b => b.TaxAmount).HasColumnType("decimal(18, 2)");
            b.Property(b => b.TaxAmountFr).HasColumnType("decimal(18, 2)");
            b.Ignore(x => x.Department);
            b.Ignore(x => x.Purchaser);
            b.Ignore(x => x.Currency);
            b.Ignore(x => x.Supplier);
            

            b.ConfigureByConvention();
        });


        builder.Entity<PurchaseOrderItem>(b =>
        {
            b.ToTable(BusinessDbProperties.DbTablePrefix + "PurchaseOrderItem", BusinessDbProperties.DbSchema);
            b.Property(b => b.AuxiliaryQuantity).HasColumnType("decimal(18, 10)");
            b.Property(b => b.BasicQuantity).HasColumnType("decimal(18, 10)");
            b.Property(b => b.Price).HasColumnType("decimal(18, 10)");
            b.Property(b => b.PriceExcludeTax).HasColumnType("decimal(18, 10)");
            b.Property(b => b.PriceIncludeTax).HasColumnType("decimal(18, 10)");
            b.Property(b => b.TaxRateValue).HasColumnType("decimal(18, 10)");
            b.Property(b => b.PriceIncludeTax).HasColumnType("decimal(18, 10)");
            b.Property(b => b.TotalAmount).HasColumnType("decimal(18, 2)");
            b.Property(b => b.TotalAmountFr).HasColumnType("decimal(18, 2)");
            b.Property(b => b.Amount).HasColumnType("decimal(18, 2)");
            b.Property(b => b.AmountFr).HasColumnType("decimal(18, 2)");
            b.Property(b => b.TaxAmount).HasColumnType("decimal(18, 2)");
            b.Property(b => b.TaxAmountFr).HasColumnType("decimal(18, 2)");
            b.Ignore(x => x.Product);
            b.Ignore(x => x.AuxiliaryUnit);
            b.Ignore(x => x.TaxRate);
            b.Ignore(x => x.ProductAuxiliaryUnit);

            b.ConfigureByConvention();
        });

        builder.Entity<PurchaseOrderAdvancePaymentItem>(b =>
        {
            b.ToTable(BusinessDbProperties.DbTablePrefix + "PurchaseOrderAdvancePaymentItem", BusinessDbProperties.DbSchema);
            b.Property(b => b.AdvancePaymentAmountFr).HasColumnType("decimal(18, 2)");
            b.Property(b => b.AdvancePaymentAmountFr).HasColumnType("decimal(18, 2)");
            b.Property(b => b.AdvancePaymentPercent).HasColumnType("decimal(18, 2)");

            b.ConfigureByConvention();
        });

        builder.Entity<PurchaseRequisitionOrder>(b =>
        {
            b.ToTable(BusinessDbProperties.DbTablePrefix + "PurchaseRequisitionOrder", BusinessDbProperties.DbSchema);

            b.ConfigureByConvention();
        });


        builder.Entity<PurchaseReceipt>(b =>
        {
            b.ToTable(BusinessDbProperties.DbTablePrefix + "PurchaseReceipt", BusinessDbProperties.DbSchema);
            b.HasIndex(x => new { x.Number, x.TenantId }).IsUnique();
            b.Property(b => b.ExchangeRate).HasColumnType("decimal(18, 10)");
            b.Property(b => b.TotalAmount).HasColumnType("decimal(18, 2)");
            b.Property(b => b.TotalAmountFr).HasColumnType("decimal(18, 2)");
            b.Property(b => b.Amount).HasColumnType("decimal(18, 2)");
            b.Property(b => b.AmountFr).HasColumnType("decimal(18, 2)");
            b.Property(b => b.TaxAmount).HasColumnType("decimal(18, 2)");
            b.Property(b => b.TaxAmountFr).HasColumnType("decimal(18, 2)");
            b.Ignore(x => x.Department);
            b.Ignore(x => x.WarehouseClerk);
            b.Ignore(x => x.Currency);

            b.ConfigureByConvention();
        });


        builder.Entity<PurchaseReceiptItem>(b =>
        {
            b.ToTable(BusinessDbProperties.DbTablePrefix + "PurchaseReceiptItem", BusinessDbProperties.DbSchema);
            b.Property(b => b.AuxiliaryQuantity).HasColumnType("decimal(18, 10)");
            b.Property(b => b.BasicQuantity).HasColumnType("decimal(18, 10)");
            b.Property(b => b.Price).HasColumnType("decimal(18, 10)");
            b.Property(b => b.PriceExcludeTax).HasColumnType("decimal(18, 10)");
            b.Property(b => b.PriceIncludeTax).HasColumnType("decimal(18, 10)");
            b.Property(b => b.TaxRateValue).HasColumnType("decimal(18, 10)");
            b.Property(b => b.PriceIncludeTax).HasColumnType("decimal(18, 10)");
            b.Property(b => b.TotalAmount).HasColumnType("decimal(18, 2)");
            b.Property(b => b.TotalAmountFr).HasColumnType("decimal(18, 2)");
            b.Property(b => b.Amount).HasColumnType("decimal(18, 2)");
            b.Property(b => b.AmountFr).HasColumnType("decimal(18, 2)");
            b.Property(b => b.TaxAmount).HasColumnType("decimal(18, 2)");
            b.Property(b => b.TaxAmountFr).HasColumnType("decimal(18, 2)");
            b.Property(b => b.ReceivableQuantity).HasColumnType("decimal(18, 10)");
            b.Ignore(x => x.Product);
            b.Ignore(x => x.ProductAuxiliaryUnit);
            b.Ignore(x => x.AuxiliaryUnit);
            b.Ignore(x => x.TaxRate);
            b.Ignore(x=>x.Warehouse);

            b.ConfigureByConvention();
        });

        builder.Entity<PurchaseOrderReceipt>(b =>
        {
            b.ToTable(BusinessDbProperties.DbTablePrefix + "PurchaseOrderReceipt", BusinessDbProperties.DbSchema);

            b.ConfigureByConvention();
        });

        builder.Entity<SaleProductPrice>(b =>
        {
            b.ToTable(BusinessDbProperties.DbTablePrefix + "SaleProductPrice", BusinessDbProperties.DbSchema);
            b.HasIndex(x => new { x.Code, x.TenantId }).IsUnique();
            b.HasIndex(x => new { x.Name, x.TenantId }).IsUnique();
            b.Ignore(x => x.Customer);
            b.Ignore(x => x.Currency);

            b.ConfigureByConvention();
        });


        builder.Entity<SaleProductPriceItem>(b =>
        {
            b.ToTable(BusinessDbProperties.DbTablePrefix + "SaleProductPriceItem", BusinessDbProperties.DbSchema);
            b.Property(b => b.Price).HasColumnType("decimal(18, 10)");
            b.Property(b => b.PriceExcludeTax).HasColumnType("decimal(18, 10)");
            b.Property(b => b.PriceIncludeTax).HasColumnType("decimal(18, 10)");
            b.Property(b => b.TaxRateValue).HasColumnType("decimal(18, 10)");
            b.Property(b => b.PriceIncludeTax).HasColumnType("decimal(18, 10)");
            b.Ignore(x => x.Product);
            b.Ignore(x => x.TaxRate);
            b.ConfigureByConvention();
        });

        builder.Entity<PurchaseProductPrice>(b =>
        {
            b.ToTable(BusinessDbProperties.DbTablePrefix + "PurchaseProductPrice", BusinessDbProperties.DbSchema);
            b.HasIndex(x => new { x.Code, x.TenantId }).IsUnique();
            b.HasIndex(x => new { x.Name, x.TenantId }).IsUnique();
            b.Ignore(x => x.Supplier);
            b.Ignore(x => x.Currency);

            b.ConfigureByConvention();
        });


        builder.Entity<PurchaseProductPriceItem>(b =>
        {
            b.ToTable(BusinessDbProperties.DbTablePrefix + "PurchaseProductPriceItem", BusinessDbProperties.DbSchema);
            b.Property(b => b.Price).HasColumnType("decimal(18, 10)");
            b.Property(b => b.PriceExcludeTax).HasColumnType("decimal(18, 10)");
            b.Property(b => b.PriceIncludeTax).HasColumnType("decimal(18, 10)");
            b.Property(b => b.TaxRateValue).HasColumnType("decimal(18, 10)");
            b.Property(b => b.PriceIncludeTax).HasColumnType("decimal(18, 10)");
            b.Ignore(x => x.Product);
            b.Ignore(x => x.TaxRate);
            b.ConfigureByConvention();
        });

        builder.Entity<SaleQuotation>(b =>
        {
            b.ToTable(BusinessDbProperties.DbTablePrefix + "SaleQuotation", BusinessDbProperties.DbSchema);
            b.HasIndex(x => new { x.Number, x.TenantId }).IsUnique();
            b.Ignore(x => x.Customer);
            b.Ignore(x => x.Currency);
            b.Ignore(x=>x.Department);
            b.Ignore(x=>x.SalePerson);

            b.ConfigureByConvention();
        });


        builder.Entity<SaleQuotationItem>(b =>
        {
            b.ToTable(BusinessDbProperties.DbTablePrefix + "SaleQuotationItem", BusinessDbProperties.DbSchema);
            b.Property(b => b.Price).HasColumnType("decimal(18, 10)");
            b.Property(b => b.PriceExcludeTax).HasColumnType("decimal(18, 10)");
            b.Property(b => b.PriceIncludeTax).HasColumnType("decimal(18, 10)");
            b.Property(b => b.TaxRateValue).HasColumnType("decimal(18, 10)");
            b.Property(b => b.PriceIncludeTax).HasColumnType("decimal(18, 10)");
            b.Ignore(x => x.Product);
            b.Ignore(x => x.TaxRate);
            b.Ignore(x=>x.AuxiliaryUnit);
            b.Ignore(x=>x.ProductAuxiliaryUnit);
            b.ConfigureByConvention();
        });

    }
}
